import LXCOM from '../../common/common.js'
import abspkg from '../../graph/action/absaction.js'
import {LGXEnums}  from '../../common/sysenum.js'

import UtilFuncs from '../../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import { CrossModLineSvc } from '../../service/crossmodlinesvc.js';
import { GlobalVar } from '../../common/globalvar.js';

import globalConst from '../../common/globalConst.js';
var global = globalConst.GlobalConst;

class LineGraphAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);

        //只有两个点的线从第一个点按下，到移动鼠标，距离超过某个值后确定第一段的走向（水平还是竖直）
        this.sureTwoPtLineDir = false;
        this.twoPtLinePreSureDir = new LXCOM.LGXVector();
    }

    act2Mouse(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        // console.log("LineGraphAction");
        let autoCrossLineFlag = false;
        var state = LGXEnums.LGXPlotState.NoneState;
        if (graph.getType() == global.TwoPtline_Topo
            || graph.getType() == global.TwoPtline_noneTopo) {
            state = this.twoPtLineAct2Mouse(mosEvnt, mousePt);
            autoCrossLineFlag = true;
        }
        else {
            state = this.commnonLineAct2Mouse(mosEvnt, mousePt);
        }
        let position = graph.getPos();
        let poscount = position.posList.length;
        if (graph.visionType == LGXEnums.lineVisionType.cross && poscount > 2) {
            let endPt = position.posList[poscount - 1];
            let endPrePt = position.posList[poscount - 2];
            let dis = funcs.distancePoint(endPrePt, endPt, 1);
            if (dis > 5) {
                CrossModLineSvc.modifyEnd(graph, mousePt, mousePt, 0);
            }
        }
        if (this._coordCorrectSvc && autoCrossLineFlag){
            this._coordCorrectSvc.act2Mouse(graph,mosEvnt, mousePt);
        }
        graph.calScope(1);
        return state;
    }

    commnonLineAct2Mouse(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        let poscount = 0;
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                var p1 = new LXCOM.LGXPoint();
                p1.x = mousePt.x;
                p1.y = mousePt.y;
                var p2 = new LXCOM.LGXPoint();
                p2.x = mousePt.x + 0.1;
                p2.y = mousePt.y;
                position.posList.push(p1);
                position.posList.push(p2);
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 1;
            }
            else {
                poscount = position.posList.length;
                var validNewPos = true;
                if (poscount > 1) {
                    var prePrePt = position.posList[poscount - 2];
                    var dis = funcs.distancePoint(prePrePt, mousePt, 1);
                    if (dis < 0.001) {
                        validNewPos = false;
                    }
                }

                if (validNewPos) {
                    var prePt = position.posList[poscount - 2];
                    var p = new LXCOM.LGXPoint();
                    var horn_vert_kind = funcs.chkHornVertKind(mousePt, prePt);
                    if (horn_vert_kind == 0) {
                        p.x = mousePt.x;
                        p.y = mousePt.y;
                    }
                    else if (horn_vert_kind == 1) {
                        p.x = mousePt.x;
                        p.y = prePt.y;
                    }
                    else if (horn_vert_kind == 2) {
                        p.x = prePt.x;
                        p.y = mousePt.y;
                    }

                    position.posList.push(p);
                    state = LGXEnums.LGXPlotState.AppendPtState;
                    graph.mouseDownTime++;
                    poscount = position.posList.length;
                }
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDbClick
            || mosEvnt.type == LGXEnums.LGXMouseMode.RMouseDown) {
            if (position.posList.length < 2) {
                graph.setUseful(0);
                return LGXEnums.LGXPlotState.ErrorState;
            }
            let idx = position.posList.length - 1;
            var finalPt = position.posList[idx-1];
            if (position.posList.length > 2){
                position.posList.splice(idx,1);
                mousePt.x = finalPt.x;
                mousePt.y = finalPt.y;
            }
            if (position.posList.length  < 2){
                graph.setUseful(0);
            }

            graph.setFinish(1);
            state = LGXEnums.LGXPlotState.FinistState;
            graph.mouseDownTime = 0;
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag) {
            if (position.posList.length < 2) {
                return LGXEnums.LGXPlotState.ErrorState;
            }
            poscount = position.posList.length;
            var curentPt = position.posList[poscount - 1];
            prePt = position.posList[poscount - 2];

            if (graph.visionType == LGXEnums.lineVisionType.cross && poscount == 2) {
                let detax = Math.abs(mousePt.x - prePt.x);
                let detay = Math.abs(mousePt.y - prePt.y);
                if (detax > detay) {
                    curentPt.y = prePt.y;
                    curentPt.x = mousePt.x;
                }
                else {
                    curentPt.x = prePt.x;
                    curentPt.y = mousePt.y;
                }
            }
            else {
                let horn_vert_kind = funcs.chkHornVertKind(mousePt, prePt);
                if (horn_vert_kind == 0) {
                    curentPt.x = mousePt.x;
                    curentPt.y = mousePt.y;
                }
                else if (horn_vert_kind == 1) {
                    curentPt.x = mousePt.x;
                    curentPt.y = prePt.y;
                }
                else if (horn_vert_kind == 2) {
                    if (!prePt) {
                        console.log("error");
                    }
                    curentPt.x = prePt.x;
                    curentPt.y = mousePt.y;
                }

                state = LGXEnums.LGXPlotState.OKState;
            }
        }

        return state;
    }

    twoPtLineAct2Mouse(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        // console.log("LineGraphAction");
        let poscount = 0;
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                var p1 = new LXCOM.LGXPoint();
                p1.x = mousePt.x;
                p1.y = mousePt.y;
                var p2 = new LXCOM.LGXPoint();
                p2.x = mousePt.x + 0.1;
                p2.y = mousePt.y;
                position.posList.push(p1);
                position.posList.push(p2);
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 1;
            }
            else {
                poscount = position.posList.length;
                var validNewPos = true;
                if (poscount > 1) {
                    var prePrePt = position.posList[poscount - 2];
                    var dis = funcs.distancePoint(prePrePt, mousePt, 1);
                    if (dis < 0.001) {
                        validNewPos = false;
                    }
                }

                if (validNewPos) {
                    var prePt = position.posList[poscount - 2];
                    var horn_vert_kind = funcs.chkHornVertKind(mousePt, prePt);
                    let idx = position.posList.length - 1;
                    var finalPt = position.posList[idx];
                    if (horn_vert_kind == 0) {
                        finalPt.x = mousePt.x;
                        finalPt.y = mousePt.y;
                    }
                    else if (horn_vert_kind == 1) {
                        finalPt.x = mousePt.x;
                        finalPt.y = prePt.y;
                    }
                    else if (horn_vert_kind == 2) {
                        finalPt.x = prePt.x;
                        finalPt.y = mousePt.y;
                    }

                    graph.setFinish(1);
                    state = LGXEnums.LGXPlotState.FinistState;
                    graph.mouseDownTime = 0;
                }
                else {
                    graph.setUseful(0);
                }
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag) {
            if (position.posList.length < 2) {
                return LGXEnums.LGXPlotState.ErrorState;
            }

            poscount = position.posList.length;
            var curentPt = position.posList[poscount - 1];
            prePt = position.posList[0];
            let horn_vert_kind = funcs.chkHornVertKind(mousePt, prePt);
            let dis = funcs.distancePoint(mousePt, prePt, 1);

            if (graph.visionType == LGXEnums.lineVisionType.cross) {
                let headPt = position.posList[0];
                let endPt = { x: mousePt.x, y: mousePt.y };
                position.clear();
                if (horn_vert_kind == 0) {
                    let midPt = { x: 0, y: 0 };
                    if (!this.sureTwoPtLineDir) {
                        let detax = Math.abs(mousePt.x - headPt.x);
                        let detay = Math.abs(mousePt.y - headPt.y);
                        if (detax > detay) {
                            midPt.x = endPt.x;
                            midPt.y = headPt.y;
                        }
                        else {
                            midPt.x = headPt.x;
                            midPt.y = endPt.y;
                        }
                        if (dis > global.SelectGap) {
                            this.sureTwoPtLineDir = true;
                            this.twoPtLinePreSureDir.setBy2Pt(headPt, midPt);
                        }
                    }
                    else {
                        if (Math.abs(this.twoPtLinePreSureDir.m) == 1) {
                            //初始确定了方向为水平
                            midPt.x = endPt.x;
                            midPt.y = headPt.y;
                        }
                        else {
                            midPt.x = headPt.x;
                            midPt.y = endPt.y;
                        }
                    }
                    position.addPoint(headPt);
                    position.addPoint(midPt);
                    position.addPoint(endPt);
                }
                else if (horn_vert_kind == 1) {
                    endPt.x = mousePt.x;
                    endPt.y = headPt.y;
                    position.addPoint(headPt);
                    position.addPoint(endPt);
                    if (dis > global.SelectGap) {
                        this.sureTwoPtLineDir = true;
                        this.twoPtLinePreSureDir.setBy2Pt(headPt, endPt);
                    }
                }
                else if (horn_vert_kind == 2) {
                    endPt.y = mousePt.y;
                    endPt.x = headPt.x;
                    position.addPoint(headPt);
                    position.addPoint(endPt);
                    if (dis > global.SelectGap) {
                        this.sureTwoPtLineDir = true;
                        this.twoPtLinePreSureDir.setBy2Pt(headPt, endPt);
                    }
                }
                else {
                    position.addPoint(headPt);
                    position.addPoint(endPt);
                }
            }
            else {
                if (horn_vert_kind == 0) {
                    curentPt.x = mousePt.x;
                    curentPt.y = mousePt.y;
                }
                else if (horn_vert_kind == 1) {
                    curentPt.x = mousePt.x;
                    curentPt.y = prePt.y;
                }
                else if (horn_vert_kind == 2) {
                    if (!prePt) {
                        console.log("error");
                    }
                    curentPt.x = prePt.x;
                    curentPt.y = mousePt.y;
                }

                state = LGXEnums.LGXPlotState.OKState;
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseUp) {
            if (GlobalVar.easySelMode && graph.getStart() == 1 && graph.mouseDownTime == 1) {
                poscount = position.posList.length;
                var validNewPos = true;
                if (poscount > 1) {
                    var prePrePt = position.posList[poscount - 2];
                    var dis = funcs.distancePoint(prePrePt, mousePt, 1);
                    if (dis < 0.001) {
                        validNewPos = false;
                    }
                }

                if (validNewPos) {
                    var prePt = position.posList[poscount - 2];
                    var horn_vert_kind = funcs.chkHornVertKind(mousePt, prePt);
                    let idx = position.posList.length - 1;
                    var finalPt = position.posList[idx];
                    if (horn_vert_kind == 0) {
                        finalPt.x = mousePt.x;
                        finalPt.y = mousePt.y;
                    }
                    else if (horn_vert_kind == 1) {
                        finalPt.x = mousePt.x;
                        finalPt.y = prePt.y;
                    }
                    else if (horn_vert_kind == 2) {
                        finalPt.x = prePt.x;
                        finalPt.y = mousePt.y;
                    }

                    graph.setFinish(1);
                    state = LGXEnums.LGXPlotState.FinistState;
                    graph.mouseDownTime = 0;

                }
                else {
                    graph.setUseful(0);
                }
            }
        }
        graph.calScope(1);
        return state;
    }

    modifyByCMD(isNew, cmd) {
        let rst = null;
        var graph = this.getRefGraph();
        var position = graph.getPos();

        if (cmd == "e") {
            graph.setFinish(1);
            graph.mouseDownTime = 0;

            let cmd = { type: 'e', value: "" };
            let nextTips = "";
            let appendToPreCMD = "";
            rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: true };

            let poscount = position.posList.length;
            let p1 = position.posList[poscount - 1];
            let p2 = position.posList[poscount - 2];

            let dis = funcs.distancePoint(p1, p2, 1);
            if (dis < global.ZERO_8E) {
                position.posList = position.posList.splice(0, poscount - 1);
            }
        }
        else if (cmd == "c") {
            graph.setFinish(1);
            graph.setClose(true);
            graph.mouseDownTime = 0;

            let oldPosList = position.posList;
            if (oldPosList.length > 3) {
                let newPosList = [];
                for (let i = 0; i < oldPosList.length - 1; i++) {
                    let t = oldPosList[i];
                    newPosList.push(t);
                }
                position.posList = newPosList;
            }

            let cmd = { type: 'c', value: "" };
            let nextTips = "";
            let appendToPreCMD = "";
            rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: true };
        }
        else {
            let coordLst = cmd.split(",");
            if (coordLst.length != 2) {
                return null;
            }
            let xdesc = coordLst[0];
            let ydesc = coordLst[1];

            if (isNew) {
                if (xdesc.indexOf("@") >= 0) {
                    return null;
                }
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);

                var p1 = new LXCOM.LGXPoint();
                p1.x = x;
                p1.y = y;
                var p2 = new LXCOM.LGXPoint();
                p2.x = x + 0.1;
                p2.y = y;
                position.addPoint(p1);
                position.addPoint(p2);
                graph.mouseDownTime = 1;
                graph.setStart(1);

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                let nextTips = "输入下一点坐标或在图中鼠标左键点击确定坐标:x,y,相对坐标使用:@x,@y。结束（e）";
                let appendToPreCMD = coordDecs;
                rst = { cmdObj: cmd, nextTips, appendToPreCMD };
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                let poscount = position.posList.length;
                let curentPt = position.posList[poscount - 1];
                let prePt = position.posList[poscount - 2];

                let isRelateCoord = false;
                let x = parseFloat(xdesc);
                let XRelateIdx = xdesc.indexOf("@");
                if (XRelateIdx >= 0) {
                    let tmp = xdesc.substr(XRelateIdx + 1, xdesc.length - XRelateIdx - 1);
                    x = prePt.x + parseFloat(tmp);
                    isRelateCoord = true;
                }

                let y = parseFloat(ydesc);
                if (isRelateCoord) {
                    y = prePt.y + y;
                }
                curentPt.x = x;
                curentPt.y = y;

                position.addPoint(curentPt);
                graph.mouseDownTime++;
                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                let nextTips = "输入下一点坐标或在图中鼠标左键点击确定坐标:x,y,相对坐标使用:@x,@y。结束（e）";
                if (graph.mouseDownTime > 2) {
                    nextTips = "输入下一点坐标或在图中鼠标左键点击确定坐标:x,y,相对坐标使用:@x,@y。闭合（c）,结束（e）";
                }
                let appendToPreCMD = coordDecs;
                rst = { cmdObj: cmd, nextTips, appendToPreCMD };
            }
        }
        graph.calScope(1);
        return rst;
    }
}

export default { LineGraphAction }
